package com.example.autoconfig.app.jdbc;
 
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;

import java.sql.*;

/**
 * HikariCP jdbc 数据库连接池工具
 */
@Slf4j
public class HikariCPUti {

    private static HikariDataSource masterDb;
    private static HikariDataSource slaveDb;

    static {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost:3339/jikeshijian");
        config.setUsername("root");
        config.setPassword("123456");
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

        masterDb = new HikariDataSource(config);
    }
    static {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost:3340/jikeshijian");
        config.setUsername("root");
        config.setPassword("123456");
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

        slaveDb = new HikariDataSource(config);
    }

   public static Connection getMasterConnection() throws SQLException {
        Connection connection = masterDb.getConnection();
       return connection;
   }

    public static Connection getSlaveConnection() throws SQLException {
        Connection connection = slaveDb.getConnection();
        return connection;
    }

}